package com.example.javaeekeshe.service.impl;

import com.example.javaeekeshe.mapper.GoodMapper;
import com.example.javaeekeshe.mapper.OrderMapper;
import com.example.javaeekeshe.pojo.FullOrderBean;
import com.example.javaeekeshe.pojo.GoodsBean;
import com.example.javaeekeshe.pojo.OrderBean;
import com.example.javaeekeshe.pojo.OrderGoodeBean;
import com.example.javaeekeshe.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DataSourceTransactionManager transactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;


    @Override
    public boolean addOrder(OrderBean orderBean) {
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);//开启事务
        try {
//            System.out.println("==========================order");
            String[] gids = orderBean.getGid().split("_");
            //减少库存
            for (String gid:gids) {
//                System.out.println("++++=="+gid);
                try {
                    goodMapper.buyGoods(Integer.parseInt(gid));
                }catch (Exception e){
//                    System.out.println("err 事务回滚"+gid);
                    transactionManager.rollback(transactionStatus);//回滚
                    goodMapper.setSate(Integer.parseInt(gid),0);//设置商品状态
                    return false;
                }

            }
            orderMapper.addOrder(orderBean);//添加订单
            transactionManager.commit(transactionStatus);     //手动提交
            return true;
        }catch (Exception e){
            transactionManager.rollback(transactionStatus);//回滚
            return false;
        }
    }

    @Override
    public List<FullOrderBean> getOrderByUid(Integer uid,Integer start) {
        List<OrderBean> orderByUid = orderMapper.getOrderByUid(uid,start*5-5);

        return orderBeanToFullOrder(orderByUid);
    }

    @Override
    public List<FullOrderBean> getAllOrder(Integer start) {
        List<OrderBean> allOrder = orderMapper.getAllOrder(start*5-5);
        return orderBeanToFullOrder(allOrder);
    }

    @Override
    public void send(Integer id) {
        orderMapper.send(id);
    }

    @Override
    public Integer rowsAll() {
        return orderMapper.rowsAll();
    }

    @Override
    public Integer rowsUid(Integer id) {
        return orderMapper.rowsUid(id);
    }

    @Override
    public void receipt(Integer id) {
        orderMapper.receipt(id);
    }


    /**
     * 根据订单信息拿到商品信息
     * @param orderByUid
     * @return
     */
    public List<FullOrderBean> orderBeanToFullOrder(List<OrderBean> orderByUid){
        List<FullOrderBean> orders=new ArrayList<FullOrderBean>();
        for (int i = 0; i < orderByUid.size(); i++) {
            List<OrderGoodeBean> orderGoodeBeans = new ArrayList<OrderGoodeBean>();
            OrderBean orderBean = orderByUid.get(i);
            String gid=orderBean.getGid();
            String[] gids = gid.split("_");
            String[] colors = orderBean.getOcolor().split("_");
            for (int j = 0; j < gids.length; j++) {
                OrderGoodeBean orderGoodeBean=new OrderGoodeBean();
                GoodsBean goodsBean = goodMapper.selectById(Integer.parseInt(gids[j]));
                orderGoodeBean.setGoodName(goodsBean.getName());
                orderGoodeBean.setGoodImg(goodsBean.getMainimg());
                orderGoodeBean.setGid(goodsBean.getId());
                orderGoodeBean.setColor(colors[j]);
                orderGoodeBeans.add(orderGoodeBean);
            }
            FullOrderBean fullOrderBean=new FullOrderBean();
            fullOrderBean.setName(orderByUid.get(i).getOname());
            fullOrderBean.setAddress(orderByUid.get(i).getOaddress());
            fullOrderBean.setState(orderByUid.get(i).getOstate());
            fullOrderBean.setPhone(orderByUid.get(i).getOphone());
            fullOrderBean.setDateTime(orderByUid.get(i).getOtime());
            fullOrderBean.setOrderGoodes(orderGoodeBeans);
            fullOrderBean.setId(orderByUid.get(i).getId());
            orders.add(fullOrderBean);
        }
        return orders;
    }
}
